﻿Module modStrings
    Public Function String_ReplaceFromKVP(ByVal str As String, ByRef kvpList As List(Of KeyValuePair(Of String, String))) _
        As String

        Dim tmpString As String = str
        Dim tmpList As List(Of KeyValuePair(Of String, String)) = kvpList

        For Each pairCurrent As KeyValuePair(Of String, String) In tmpList

            Dim tmpKey As String = pairCurrent.Key
            Dim tmpValue As String = pairCurrent.Value

            tmpString = tmpString.Replace(tmpKey, tmpValue)

        Next

        Return tmpString
    End Function

    Public Function String_IsEmpty(stringText As String) As Boolean

        If (stringText Is Nothing) Then Return True
        If (stringText.Length = 0) Then Return True

        Return False
    End Function

    Public Function String_ReplaceToKVP(ByVal str As String, ByRef kvpList As List(Of KeyValuePair(Of String, String))) _
        As String

        Dim tmpString As String = str
        Dim tmpList As List(Of KeyValuePair(Of String, String)) = kvpList

        For Each pairCurrent As KeyValuePair(Of String, String) In tmpList

            Dim tmpKey As String = pairCurrent.Key
            Dim tmpValue As String = pairCurrent.Value

            tmpString = tmpString.Replace(tmpValue, tmpKey)

        Next

        Return tmpString
    End Function

    Public Function String_Equals(ByVal compare1 As Object, ByVal compare2 As Object,
                                  Optional ByVal optIgnoreCase As Boolean = True,
                                  Optional ByVal optTrim As Boolean = True) As Boolean

        If optTrim = True Then
            compare1 = Trim(compare1)
            compare2 = Trim(compare2)
        End If

        If String.Equals(compare1, compare2, StringComparison.CurrentCultureIgnoreCase) Then

            Return True

        Else

            Return False

        End If
    End Function

    Public Function String_Substring(ByVal stringText As String, ByVal indexStart As Integer, ByVal indexLength As Integer) _
        As String

        If (stringText Is Nothing) Then

            Return ""

        End If

        If (stringText.Length < (indexStart + indexLength)) Then

            Return ""

        Else

            Return stringText.Substring(indexStart, indexLength)

        End If
    End Function

    Public Function String_TextAfter(ByVal stringText As String, ByVal stringBefore As String) As String

        Dim _start As Integer
        Dim _length As Integer

        _start = stringText.IndexOf(stringBefore, StringComparison.Ordinal) + stringBefore.Length
        _length = (stringText.Length - _start)

        Return stringText.Substring(_start, _length)

    End Function


    Public Function Integer_Between(ByRef stringText As String, ByVal stringBefore As String,
                                    Optional ByVal stringAfter As String = Nothing) As Integer?

        Dim _start As Integer
        Dim _length As Integer
        Dim _substring As String
        Dim _return As Integer? = Nothing

        If stringAfter = Nothing Then

            stringAfter = stringBefore

        End If

        _start = stringText.IndexOf(stringBefore, StringComparison.Ordinal) + stringBefore.Length
        _length = stringText.Substring(_start).IndexOf(stringAfter, StringComparison.Ordinal)
        _substring = stringText.Substring(_start, _length)

        If (IsNumeric(_substring)) Then _return = CInt(_substring)

        Return _return
    End Function

    Public Function String_Between(ByRef stringText As String, ByVal stringBefore As String,
                                   Optional ByVal stringAfter As String = Nothing) As String

        Dim _start As Integer
        Dim _length As Integer

        If stringAfter = Nothing Then

            stringAfter = stringBefore

        End If

        _start = stringText.IndexOf(stringBefore, StringComparison.Ordinal) + stringBefore.Length
        _length = stringText.Substring(_start).IndexOf(stringAfter, StringComparison.Ordinal)

        Return stringText.Substring(_start, _length)
    End Function

    Public Function String_CharCount(ByVal stringText As String, ByVal stringChar As String,
                                     Optional boolCaseSensitive As Boolean = False) As Integer

        Dim _count As Integer

        If boolCaseSensitive = False Then stringText = stringText.ToLower

        For i As Integer = 0 To (stringText.Length - 1)

            If stringText.Chars(i) = stringChar Then
                _count = _count + 1
            End If

        Next

        Return _count
    End Function

    Public Function String_Quote(ByVal stringText As String) As String

        Return """" & stringText & """"
    End Function
End Module